import requests
import json
import time
import updatacooies
from  somecookie import readCookie


# todo  检测乱码 如果过word中乱码超过40% 反回False
def isChinese(word):
    right = 0
    for ch in word:
        if '\u4e00' <= ch <= '\u9fff' or '0' <= ch <= '9' or 'a' <= ch <= 'z' or 'A' <= ch <= 'Z':
            right += 1
    if right / len(word) > 0.4:#文字占有率>40%则不算乱码
        return True
    return False


def main(uid):
    headers = {
        'authority': 'weibo.com',
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'client-version': 'v2.34.93',
        # Requests sorts cookies= alphabetically
        # 'cookie': 'SINAGLOBAL=2981731535674.7163.1662034725865; ULV=1662034725868:1:1:1:2981731535674.7163.1662034725865:; SSOLoginState=1662114006; XSRF-TOKEN=qjUbu9g0V3-_hU5SpEW2hwIs; WBPSESS=Dt2hbAUaXfkVprjyrAZT_A-xy9vBVM8uz6G21zsaO9q8XSgnQxleiqynfVGQd9113YZSjA2yYaj7KKx0MOwn5vKa-mMcA2OZdqjaOhfIRxXMazkNAiC94krdwOnKK0y9BHBdblfGMn5iboFzU6PVJ-7sYPZv4tI56RS1xcOk3pvR1EYrcB3HwdeTVU4opMCr8pFOSnNE4ky5Rf1_P6XtrQ==; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFfVblZ6Vcs2ikRYqBrTWMD5JpX5KMhUgL.FoMpS0BNe0qpSKz2dJLoI0YLxK-L1K5L12-LxK-LBKBLBK.LxKBLBonL1h5LxKnLBo-LBoMLxKqL1K.L1KzLxK-LBKBLB-2LxK.L1KnLBoet; ALF=1693738161; SCF=AvBXETlJ-dfVEJ3UJtBMxHTn6JDcvInUEbB89dp7htJI4RVulcG459hgdGJ3S8aTWvyQhIfrroNCnhran1ETlRw.; SUB=_2A25OF0FiDeRhGeFP7FYW8yjNzj6IHXVtZTWqrDV8PUNbmtANLXjWkW9NQRhUGT9tEp2p1sJUAeGX_8l5e_9VyW5_',
        'pragma': 'no-cache',
        'referer': 'https://weibo.com/u/3669102477',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'server-version': 'v2022.09.02.2',
        'traceparent': '00-a1a5b3c9b47ed4b49f2d58db8e6877b0-b2eb6337b69597c7-00',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
        'x-xsrf-token': 'qjUbu9g0V3-_hU5SpEW2hwIs',
    }
    since_id = ''
    f = open('word', 'a+', encoding='utf-8')
    for page in range(1, 1000):


        cookies = {
            'SUB': readCookie('SUB'),
        }
        params = {
            'uid': uid,
            'page': str(page),
            'feature': '0',
            'since_id': since_id,
        }
        print(params)
        response = requests.get('https://weibo.com/ajax/statuses/mymblog', params=params, cookies=cookies,headers=headers)
        data = json.loads(response.text)['data']
        since_id = data['since_id']
        print(str(page) + "************************************************************************************")
        for i in data['list']:
            print(i['text_raw'])
            if isChinese(i['text_raw']):
                f.write(i['text_raw'])
                f.write('\n***********************************************************************\n')
            else:
                s = open('param', 'a', encoding='utf-8')
                s.write(str(params) + '\n')
                s.close()
                break
    f.close()

uid='2913767243'
main(uid)


